package com.qq.BFMRSE.Hdfs;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

import com.qq.BFMRSE.util.BFConstants;

public class hdfsConf {
	public static void initProperHDFS(Configuration conf)
	{
		conf.setInt("dfs.replication", BFConstants.HDFSREPLICATION);
		conf.setInt("dfs.block.size", BFConstants.HDFSBLOCKSIZE);
	}
	/**
	 * get all information about the dataNode
	 * @return
	 * @throws IOException
	 */
	public static DatanodeInfo[] getDataNodeInfo() throws IOException
	{
		Configuration conf=getConf();
		FileSystem fs=FileSystem.get(conf);
		DistributedFileSystem dfs=(DistributedFileSystem)fs;
		DatanodeInfo []info=dfs.getDataNodeStats();
		for(int i=0;i<info.length;i++)
		{
			System.out.println(info[i].getHostName()+":"+info[i].getIpAddr());
		}
		return info;
	}
	/**
	 * get the configuration of hdfs
	 * @return
	 */
	public static Configuration getConf()
	{
		Configuration conf=new Configuration();
		conf.set("fs.default.name",BFConstants.HADOOP_PATH);
		return conf;
	}
	/**
	 * get all locations information of the specific file 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static BlockLocation[] getLocationOfFile(String fileName) throws IOException
	{
		String  pathName=BFConstants.HADOOP_PATH+File.separator+fileName;
		Configuration conf=new Configuration();
		FileSystem hdfs=FileSystem.get(URI.create(pathName), conf);
		Path path=new Path(pathName);
		FileStatus fStatus=hdfs.getFileStatus(path);
		BlockLocation[] bl=hdfs.getFileBlockLocations(fStatus, 0, fStatus.getLen());
		return bl;
	}

}